#pragma once
#include "util/noncopyable.h"

#include <pthread.h>
namespace net_tools
{
  namespace util
  {

    class Mutex : noncopyable
    {
    public:
      Mutex()
      {
        pthread_mutex_init(&mutex_, NULL);
      };
      ~Mutex()
      {
        pthread_mutex_destroy(&mutex_);
      };
      void Lock()
      {
        pthread_mutex_lock(&mutex_);
      }
      void Unlock()
      {
        pthread_mutex_unlock(&mutex_);
      }
      pthread_mutex_t *Get_mutex() { return &mutex_; }

    private:
      pthread_mutex_t mutex_;
    };
  } // namespace util

  class MutexLock : util::noncopyable
  {
  public:
    explicit MutexLock(util::Mutex &mutex) : mutex_(mutex)
    {
      mutex_.Lock();
    }
    ~MutexLock()
    {
      mutex_.Unlock();
    }

  private:
    util::Mutex &mutex_;
  };

} // namespace net_tools